package com.lxk.finance.service.impl;

import cn.hutool.core.util.IdUtil;
import com.lxk.finance.domain.vo.AccUserAccountVo;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import cn.hutool.core.collection.CollUtil;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.lxk.finance.domain.bo.AccUserAccountRecordBo;
import com.lxk.finance.domain.vo.AccUserAccountRecordVo;
import com.lxk.finance.domain.AccUserAccountRecord;
import com.lxk.finance.mapper.AccUserAccountRecordMapper;
import com.lxk.finance.service.IAccUserAccountRecordService;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * 账户流水Service业务层处理
 *
 * @author LXK-RY5
 * @date 2024-07-08
 */
@RequiredArgsConstructor
@Service
public class AccUserAccountRecordServiceImpl implements IAccUserAccountRecordService {

    private final AccUserAccountRecordMapper baseMapper;


    @Override
    public AccUserAccountRecordBo addRecord(String orderSn, BigDecimal amount,
                                            String extraInfo,
                                            AccUserAccountRecordBo.IncomeStatus incomeStatus,
                                            AccUserAccountVo accUserAccountVo) {
        AccUserAccountRecordBo add = new AccUserAccountRecordBo();
        add.setOrderSn(orderSn);
        add.setAccountId(accUserAccountVo.getId());
        add.setUserId(accUserAccountVo.getUserId());
        add.setAccountType(accUserAccountVo.getAccountType());
        add.setAccountSn(IdUtil.getSnowflakeNextIdStr());
        add.setExtraInfo(extraInfo);
        add.setIncomeStatus(incomeStatus.getCode());
        add.setAmount(amount);
        add.setUserType(accUserAccountVo.getUserType());
        add.setLeftAmount(accUserAccountVo.getAmount().add(amount));
        this.insertByBo(add);
        return add;
    }

   /**
    * 查询IdList账户流水Map
    */
    @Override
    public Map<Long, AccUserAccountRecordVo> queryMapByIdList(List<Long> idList) {
        return this.queryByIdList(idList).stream().collect(Collectors.toMap(AccUserAccountRecordVo::getId, v -> v));
    }

    /**
     * 查询账户流水Map
     */
    @Override
    public Map<Long, AccUserAccountRecordVo> queryMap(AccUserAccountRecordBo bo) {
        return  this.queryList(bo).stream().collect(Collectors.toMap(AccUserAccountRecordVo::getId, v -> v));
    }

    /**
     * 查询账户流水IdList
     */
    @Override
    public List<AccUserAccountRecordVo> queryByIdList(List<Long> idList) {
        if (CollUtil.isEmpty(idList)) {
            return CollUtil.newArrayList();
        }
        LambdaQueryWrapper<AccUserAccountRecord> lqw = Wrappers.lambdaQuery();
        lqw.in(AccUserAccountRecord::getId, idList);
       return baseMapper.selectVoList(lqw);
    }

    /**
     * 查询账户流水
     *
     * @param id 主键
     * @return 账户流水
     */
    @Override
    public AccUserAccountRecordVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询账户流水列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 账户流水分页列表
     */
    @Override
    public TableDataInfo<AccUserAccountRecordVo> queryPageList(AccUserAccountRecordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AccUserAccountRecord> lqw = buildQueryWrapper(bo);
        Page<AccUserAccountRecordVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的账户流水列表
     *
     * @param bo 查询条件
     * @return 账户流水列表
     */
    @Override
    public List<AccUserAccountRecordVo> queryList(AccUserAccountRecordBo bo) {
        LambdaQueryWrapper<AccUserAccountRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AccUserAccountRecord> buildQueryWrapper(AccUserAccountRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AccUserAccountRecord> lqw = Wrappers.lambdaQuery();
        lqw.orderByDesc(AccUserAccountRecord::getId);
        lqw.eq(bo.getUserId() != null, AccUserAccountRecord::getUserId, bo.getUserId());
        lqw.eq(bo.getAccountType() != null, AccUserAccountRecord::getAccountType, bo.getAccountType());
        lqw.eq(bo.getAmount() != null, AccUserAccountRecord::getAmount, bo.getAmount());
        lqw.eq(bo.getAccountId() != null, AccUserAccountRecord::getAccountId, bo.getAccountId());
        lqw.eq(StringUtils.isNotBlank(bo.getAccountSn()), AccUserAccountRecord::getAccountSn, bo.getAccountSn());
        lqw.eq(StringUtils.isNotBlank(bo.getOrderSn()), AccUserAccountRecord::getOrderSn, bo.getOrderSn());
        lqw.eq(StringUtils.isNotBlank(bo.getExtraInfo()), AccUserAccountRecord::getExtraInfo, bo.getExtraInfo());
        lqw.eq(bo.getLeftAmount() != null, AccUserAccountRecord::getLeftAmount, bo.getLeftAmount());
        lqw.eq(bo.getIncomeStatus() != null, AccUserAccountRecord::getIncomeStatus, bo.getIncomeStatus());
        lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
            AccUserAccountRecord::getCreateTime ,params.get("beginCreateTime"), params.get("endCreateTime"));
        lqw.between(params.get("beginUpdateTime") != null && params.get("endUpdateTime") != null,
            AccUserAccountRecord::getUpdateTime ,params.get("beginUpdateTime"), params.get("endUpdateTime"));
        lqw.eq(StringUtils.isNotBlank(bo.getUserType()), AccUserAccountRecord::getUserType, bo.getUserType());
        lqw.eq(bo.getWithdrawStatus() != null, AccUserAccountRecord::getWithdrawStatus, bo.getWithdrawStatus());
        return lqw;
    }

    /**
     * 新增账户流水
     *
     * @param bo 账户流水
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(AccUserAccountRecordBo bo) {
        AccUserAccountRecord add = MapstructUtils.convert(bo, AccUserAccountRecord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改账户流水
     *
     * @param bo 账户流水
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(AccUserAccountRecordBo bo) {
        AccUserAccountRecord update = MapstructUtils.convert(bo, AccUserAccountRecord.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AccUserAccountRecord entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除账户流水信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
